import _ from "lodash";
export default class RuntimeHandleDataService {
    context;
    cpasData;
    constructor(context) {
        this.context = context;
    }
    recoverSession(cpasData) {
        this.cpasData = cpasData;
        this.recoverHideColumnSession();
        this.recoverDismantleTableSession();
    }
    initReverseInfo() {
        const { header, data, cellsMeta, tableConfig } = this.cpasData;
        const reverseInfo = tableConfig?.reverseInfo;
        const isReverse = reverseInfo?.isReverse;
        const userAction = localStorage.userAction;
        if (reverseInfo && userAction === "clickedImport") {
            this.context.menuToolRef.current.needReverseGroupInfo =
                reverseInfo.needReverseGroupInfo;
            this.context.menuToolRef.current.setState({
                isReverse: reverseInfo.isReverse,
            });
            setTimeout(() => {
                localStorage.removeItem("userAction");
            }, 1000);
        }
    }
    recoverHideColumnSession() {
        const { header, data, cellsMeta, tableConfig } = this.cpasData;
        // 根据下标寻找新的 key
        // const hideColumnArr = zlTableConfig.hideColumnArr;
        const hideColumnArr = tableConfig.hideColumnArr;
        //const headerColumn = dsTableRef.current?.getCurrentColumnSource();
        const headerColumn = this.context.getCurrentColumnSource();
        console.log("===================headerColumn=================", headerColumn);
        // 导入时 header 元素的key 变了 所以要根据隐藏列下标计算新的 key 值
        let newHideColumnKeyArr = [];
        for (let i = 0; i < hideColumnArr?.length; i++) {
            const hideColumnIndex = hideColumnArr[i]; // 2
            const newKey = headerColumn[hideColumnIndex].key;
            newHideColumnKeyArr.push(newKey);
        }
        //zlTableConfig.hideColumnKeyArr = newHideColumnKeyArr;
        if (newHideColumnKeyArr.length > 0) {
            tableConfig.hideColumnKeyArr = newHideColumnKeyArr;
        }
        if (hideColumnArr && hideColumnArr.length > 0) {
            sessionStorage.allHeaderColumn = JSON.stringify(tableConfig.allHeaderColumn);
            sessionStorage.hideColumnKeyArr = JSON.stringify(tableConfig.hideColumnKeyArr);
            sessionStorage.hideColumnArr = JSON.stringify(tableConfig.hideColumnArr);
        }
    }
    recoverDismantleTableSession() {
        const { header, data, cellsMeta, tableConfig } = this.cpasData;
        const dismantleTableMode = tableConfig.dismantleTableMode;
        const rule = tableConfig.rule;
        if (dismantleTableMode && rule) {
            sessionStorage.hideColumnKeyArr = JSON.stringify(tableConfig.hideColumnKeyArr);
            sessionStorage.rule = JSON.stringify(tableConfig.rule);
            sessionStorage.dismantleTableMode = JSON.stringify(dismantleTableMode);
            sessionStorage.allHeaderColumn = JSON.stringify(tableConfig.allHeaderColumn);
            sessionStorage.hideColumnArr = JSON.stringify(tableConfig.hideColumnArr);
        }
    }
    reDismantleTable() {
        const { header, data, cellsMeta, tableConfig } = this.cpasData;
        const cpasDataCopy = _.cloneDeep(this.cpasData);
        const dismantleTableMode = tableConfig.dismantleTableMode;
        const rule = tableConfig.rule;
        if (dismantleTableMode && rule) {
            sessionStorage.hideColumnKeyArr = JSON.stringify(tableConfig.hideColumnKeyArr);
            sessionStorage.rule = JSON.stringify(tableConfig.rule);
            sessionStorage.dismantleTableMode = JSON.stringify(dismantleTableMode);
            sessionStorage.allHeaderColumn = JSON.stringify(tableConfig.allHeaderColumn);
            sessionStorage.hideColumnArr = JSON.stringify(tableConfig.hideColumnArr);
            const gridDataArr = this.context.menuToolRef.current.collationModeService.divideTable(cpasDataCopy, rule);
            sessionStorage.importDismantledTableJson = JSON.stringify(true);
            console.log("gridDataArr:", gridDataArr);
            if (gridDataArr.length > 0) {
                this.context.menuToolRef.current.showDividedTable(gridDataArr);
            }
            // 解决用户点击导入json文件 导入的是拆表后的json数据 样式不显示的问题  渲染完毕以后移除
            setTimeout(() => {
                localStorage.removeItem("userAction");
                // sessionStorage.removeItem("importDismantledTableJson");
            }, 1000);
        }
    }
}
